<template>
	<view class="content">
		<div class="mui-content-padded">
			<!-- 列表组件 -->
			<cc-listView :productList="projectList" :isKHJL='isKHJL' @click.stop="goProDetail"
				@del.stop="del"></cc-listView>
		</div>
		<!--  totalNum: 条目总数量  pageCount:设置分页单页数据数量  curPageNum:设置当前页-->
		<cc-listPageView :totalNum="totalNum" pageCount="6" :curPageNum="curPageNum" @pageClick="pageClick">
		</cc-listPageView>
		<view>
			<!-- 提示窗示例 -->
			<uni-popup ref="alertDialog" type="dialog">
				<uni-popup-dialog :type="dialogMsgType" cancelText="取消" confirmText="确定" title="提示" content="确认删除此客户!"
					@confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
			</uni-popup>
		</view>
		<!-- 提示信息弹窗 -->
		<uni-popup ref="message" type="message">
			<uni-popup-message :type="msgType" :message="messageText" :duration="2000"></uni-popup-message>
		</uni-popup>
	</view>
</template>
<script>
	import {
		paginQuery,
		deleteById
	} from '@/api/customer'
	export default {
		components: {},
		data() {
			return {
				isKHJL: false, // 是否是客户经理
				totalNum: 0,
				curPageNum: 1,
				// 列表数组
				projectList: [],
				type: 'center',
				dialogMsgType: 'success',
				msgType: 'success',
				messageText: '这是一条成功提示',
				value: '',
				customerId: ''
			}
		},
		onLoad() {
			const userInfo = this.$store.state.user
			for (const role of userInfo.roles) {
				if (['fb7e4f4c42ff1ce755334a352b5e3efe', 'c4d86113fa7bcfed6d0053788ed01cdb'].includes(role.roleId)) {
					this.isKHJL = true
				}
			}
			this.requestData();
		},
		onUnload() {
			uni.switchTab({
				url: '/pages/work/index'
			});
		},
		methods: {
			// 列表条目点击事件
			goProDetail(item) {
				this.$tab.navigateTo(`/pages/customer/add?customerId=${item.id}`)
			},
			del(item) {
				this.customerId = item.id
				this.dialogToggle('info')
			},
			dialogToggle(type) {
				this.dialogMsgType = type
				this.$refs.alertDialog.open()
			},
			dialogConfirm() {
				deleteById({
					id: this.customerId
				}).then(res => {
					this.msgType = 'success'
					this.messageText = '删除成功!'
					this.$refs.message.open()
					this.requestData();
				}).catch(error => {
					console.log(error)
				})
			},
			dialogClose() {
				this.msgType = 'success'
				this.messageText = `取消成功!`
				this.$refs.message.open()
			},
			// 分页事件
			pageClick(tag) {
				if (tag === 0) {
					// 上一页 (不等于第一页)
					if (this.curPageNum > 1) {
						this.curPageNum--;
						this.requestData();
					}
				} else {
					// 下一页 (不等于最后一页)
					if (this.totalNum > (this.curPageNum * 6)) {
						this.curPageNum++;
						this.requestData();
					}
				}
			},
			requestData() {
				let reqData = {
					pageSize: 6,
					pageNo: this.curPageNum
				}
				paginQuery(reqData).then(res => {
					const data = res.data
					this.projectList = data.rows
					this.totalNum = data.total;
				}).catch(error => {
					console.log(error)
				})
			}
		}
	}
</script>


<style>
	page {

		background-color: #f7f7f7;
	}

	.content {
		display: flex;
		flex-direction: column;

	}

	.mui-content-padded {
		margin: 0px 14px;
		/* background-color: #ffffff; */
	}
</style>